package com.example.capsule.controller;

import com.example.capsule.service.SmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/config")
@Tag(name = "配置管理API", description = "系统配置管理接口")
public class ConfigController {

    private final SmsService smsService;

    @Value("${sms.test-mode.enabled:true}")
    private boolean testModeEnabled;

    @Value("${sms.test-mode.code:123456}")
    private String testCode;

    public ConfigController(SmsService smsService) {
        this.smsService = smsService;
    }

    @GetMapping("/test-mode")
    @Operation(summary = "获取测试模式状态", description = "获取当前测试模式状态信息")
    public Map<String, Object> getTestModeStatus() {
        Map<String, Object> response = new HashMap<>();
        response.put("testModeEnabled", testModeEnabled);
        response.put("testCode", testCode);
        response.put("systemTestMode", smsService.isTestModeEnabled());
        response.put("systemTestCode", smsService.getTestCode());
        response.put("message", testModeEnabled ?
                "测试模式已启用，使用 " + testCode + " 作为验证码" : "生产模式");
        return response;
    }

    @PostMapping("/test-mode/toggle")
    @Operation(summary = "切换测试模式", description = "启用或禁用测试模式（需要管理员权限）")
    public Map<String, Object> toggleTestMode(@RequestParam boolean enabled) {
        Map<String, Object> response = new HashMap<>();

        // 注意：生产环境中应该添加权限验证
        // 这里只是示例，实际应该使用配置中心或数据库存储状态

        response.put("success", true);
        response.put("testModeEnabled", enabled);
        response.put("message", enabled ? "测试模式已启用" : "测试模式已禁用");

        // 记录操作日志
        System.out.println("测试模式状态变更: " + (enabled ? "启用" : "禁用"));

        return response;
    }

    @GetMapping("/system-info")
    @Operation(summary = "获取系统信息", description = "获取系统配置信息")
    public Map<String, Object> getSystemInfo() {
        Map<String, Object> response = new HashMap<>();
        response.put("testMode", testModeEnabled);
        response.put("testCode", testCode);
        response.put("timestamp", System.currentTimeMillis());
        response.put("status", "正常运行");
        return response;
    }
}